
#include <iostream>
#include <sstream>
#include "AdvancedAutoBalance.h"

using namespace std;

AutoBalanceTool* AutoBalanceTool::instance()
{
    static AutoBalanceTool instance;
    return &instance;
};

std::map<int, std::string> AutoBalanceTool::loadTimeSeperated(string timeSeperated)
{
    std::stringstream timeSeperatedStream;
    timeSeperatedStream.str(timeSeperated);
    timeSeperatedMap[0]="00:00:00";
    int i=1;
    while (std::getline(timeSeperatedStream, timeSeperated, '|')) 
    {
        string spStr = timeSeperated.c_str();
        timeSeperatedMap[i]= spStr + ":00";
        i++;
    }
    timeSeperatedMap[i]="24:00:00";
    return timeSeperatedMap;
};

string AutoBalanceTool::getTimeNow()
{
    time_t now = time(0);
	struct tm *info = localtime(&now);
    string hour = info->tm_hour < 10 ? "0"+std::to_string(info->tm_hour) : std::to_string(info->tm_hour);
    string min = info->tm_min < 10 ? "0"+std::to_string(info->tm_min) : std::to_string(info->tm_min);
    string sec = info->tm_sec < 10 ? "0"+std::to_string(info->tm_sec) : std::to_string(info->tm_sec);
    string timeStr = hour+":"+min+":"+sec;
    return timeStr;
};

int AutoBalanceTool::getBelongWhichIndex(string timeStr){
    for (int index=0; index<timeSeperatedMap.size(); index++) {
        if(timeSeperatedMap[index] <= timeStr && timeStr < timeSeperatedMap[index+1]){
            return index;
        }
    }
    return -1;
};

std::map<int, float> AutoBalanceTool::getCommonSeperated(std::string toBeSeperatedStr)
{
    std::map<int, float> retMap;
    std::stringstream seperatedStream;
    seperatedStream.str(toBeSeperatedStr);
    int i=0;
    while (std::getline(seperatedStream, toBeSeperatedStr, '|')) 
    {
        float spStr = std::stof(toBeSeperatedStr.c_str());
        retMap[i]= spStr;
        i++;
    }
    return retMap;
};


